<template>
  <div class="app-container">
    <el-row :gutter="20" style="height: 800px">
      <!--用户数据-->
      <el-col :span="24" :xs="24" style="float: left">

        <el-card class="box-card" style="width: 500px; float: left">
          <div slot="header" class="clearfix">
            <span>贷款的学生</span>
            <el-button style="float: right; padding: 3px 0" @click="resetQuery" type="text">重置</el-button>
          </div>
          <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="130px">
            <el-form-item prop="phonenumber" label="请输入机号码搜索">
              <el-input
                v-model="queryParams.phonenumber"
                placeholder="请输入手机号码"
                clearable
                size="small"
                style="width: 210px"
                @keyup.enter.native="handleQuery"
              />
              <el-button style="margin-left: 20px" type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            </el-form-item>
          </el-form>
          <el-table @row-click="rowClick" :cell-style="{padding:'10px'}" v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="学生姓名" prop="userName" width="120" align="center" />
            <el-table-column label="学生昵称" align="center" prop="nickName" />
            <el-table-column label="手机号码" align="center" prop="phonenumber" width="180" />
            <el-table-column label="创建时间" align="center" prop="createTime" width="200">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime) }}</span>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-card>

        <el-card class="box-card" style="width: 500px; float: left; margin-left: 20px">
          <div slot="header" class="clearfix">
            <span>还款订单记录</span>
            <el-button style="float: right; padding: 3px 0" @click="resetQuery" type="text">重置</el-button>
          </div>
          <el-table
            :data="payOrderList"
            style="width: 100%">
            <el-table-column
              align="center"
              width="140"
              label="系统订单号"
              prop="orderNo">
            </el-table-column>
            <el-table-column type="expand">
              <template slot-scope="props">
                <el-form label-position="left" inline class="demo-table-expand">
                  <el-form-item label="商户订单号">
                    <span>{{ props.row.outTradeNo }}</span>
                  </el-form-item>
                  <el-form-item label="支付订单号">
                    <span>{{ props.row.payNo }}</span>
                  </el-form-item>
                  <el-form-item label="支付金额">
                    <span>{{ props.row.money }}</span>
                  </el-form-item>
                  <el-form-item label="商品简称">
                    <span>{{ props.row.body }}</span>
                  </el-form-item>
                  <el-form-item label="支付商户号">
                    <span>{{ props.row.mchid }}</span>
                  </el-form-item>
                </el-form>
              </template>
            </el-table-column>
            <el-table-column
              width="122"
              align="center"
              label="商户订单号"
              prop="outTradeNo">
            </el-table-column>
            <el-table-column
              width="120"
              align="center"
              label="支付订单号"
              prop="payNo">
            </el-table-column>
            <el-table-column
              align="center"
              label="支付金额"
              prop="money">
            </el-table-column>
            <el-table-column
              width="120"
              align="center"
              label="支付商户号"
              prop="mchid">
            </el-table-column>
            <el-table-column
              width="120"
              align="center"
              label="商品简称"
              prop="body">
            </el-table-column>
            <el-table-column
              align="center"
              label="支付渠道"
              prop="payChannel">
            </el-table-column>
            <el-table-column
              align="center"
              label="支付状态"
              prop="payStatus">
              <template slot-scope="scope">
                <svg v-if="scope.row.payStatus === 1" style="width: 24px; height: 24px" t="1604766596398" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2973" width="200" height="200"><path d="M512 981.333333C252.8 981.333333 42.666667 771.2 42.666667 512S252.8 42.666667 512 42.666667s469.333333 210.133333 469.333333 469.333333-210.133333 469.333333-469.333333 469.333333z m-50.432-326.101333L310.613333 504.32a32 32 0 0 0-45.226666 45.226667l174.72 174.762666a32.341333 32.341333 0 0 0 0.341333 0.341334l0.256 0.213333a32 32 0 0 0 50.048-6.144l337.450667-379.605333a32 32 0 1 0-47.872-42.496l-318.762667 358.613333z" fill="#52C41A" p-id="2974"></path></svg>
                <svg v-else style="width: 20px; height: 20px" t="1604766752939" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4359" width="200" height="200"><path d="M549.044706 512l166.189176-166.249412a26.383059 26.383059 0 0 0 0-36.98447 26.383059 26.383059 0 0 0-37.044706 0L512 475.015529l-166.249412-166.249411a26.383059 26.383059 0 0 0-36.98447 0 26.383059 26.383059 0 0 0 0 37.044706L475.015529 512l-166.249411 166.249412a26.383059 26.383059 0 0 0 0 36.98447 26.383059 26.383059 0 0 0 37.044706 0L512 548.984471l166.249412 166.249411a26.383059 26.383059 0 0 0 36.98447 0 26.383059 26.383059 0 0 0 0-37.044706L548.984471 512zM512 1024a512 512 0 1 1 0-1024 512 512 0 0 1 0 1024z" fill="#E84335" p-id="4360"></path></svg>
              </template>
            </el-table-column>
            <el-table-column
              width="140"
              align="center"
              label="下单时间"
              prop="addTime">
            </el-table-column>
            <el-table-column
              align="center"
              label="支付方式"
              prop="payType">
            </el-table-column>
            <el-table-column
              align="center"
              label="附加数据"
              prop="attach">
            </el-table-column>
          </el-table>

        </el-card>
      </el-col>

      <el-col style="margin-top: 10px">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>贷款数据</span>
            <el-button style="float: right; padding: 3px 0" type="text" @click="resetQuery">重置</el-button>
          </div>
          <el-table v-loading="loading" :data="creditList" @selection-change="handleSelectionChange">
            <el-table-column width="120" label="年度" align="center" prop="duringYear" fixed="left" />
            <el-table-column label="借款人信息(学生)" align="center" >
              <el-table-column width="120" label="QQ" align="center" prop="qq" />
              <el-table-column width="180" label="学生身份证" align="center" prop="cardIdentity" />
              <el-table-column width="120" label="户口性质" align="center" prop="residence" />
              <el-table-column width="120" label="毕业中学" align="center" prop="medium" :show-overflow-tooltip="true" />
              <el-table-column width="120" label="户籍" align="center" prop="household" :show-overflow-tooltip="true" />
              <el-table-column width="120" label="申贷原因" align="center" prop="applyReason" :show-overflow-tooltip="true" />
              <el-table-column width="180" label="学生身份证有效起始日期" align="center" prop="studentEffectiveCardIdentity">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.studentEffectiveCardIdentity) }}</span>
                </template>
              </el-table-column>
              <el-table-column width="180" label="学生身份证有效结束日期" align="center" prop="studentInvalidCardIdentity" >
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.studentInvalidCardIdentity) }}</span>
                </template>
              </el-table-column>
            </el-table-column>
            <el-table-column width="120" label="共同借款人" align="center" :show-overflow-tooltip="true" >
              <el-table-column width="120" label="共同借款人姓名" align="center" prop="guarantor" :show-overflow-tooltip="true" />
              <el-table-column width="180" label="共同借款人联系电话" align="center" prop="guaPhone" :show-overflow-tooltip="true" />
              <el-table-column width="120" label="关系" align="center" prop="relation" :show-overflow-tooltip="true" />
              <el-table-column width="180" label="共同借款人身份证号" align="center" prop="guaCardIdentity" :show-overflow-tooltip="true" />
              <el-table-column width="120" label="健康状态" align="center" prop="heath" :show-overflow-tooltip="true" />
            </el-table-column>
            <el-table-column width="240" label="资助中心" align="center" >
              <template slot-scope="scope">
                <span>{{ scope.row.supportCenterProvince }} {{ scope.row.supportCenterCity }} {{ scope.row.supportCenterArea }}</span>
              </template>
            </el-table-column>
            <el-table-column width="240" label="入学前户籍所在地" align="center" >
              <template slot-scope="scope">
                <span>{{ scope.row.householdOfProvince }} {{ scope.row.householdOfCity }} {{ scope.row.householdOfArea }} {{ scope.row.householdOfCounty }} {{ scope.row.householdOfDetail }}</span>
              </template>
            </el-table-column>
            <el-table-column label="家庭住址" align="center" :show-overflow-tooltip="true" >
              <el-table-column width="240" label="家庭住址所在地" align="center" >
                <template slot-scope="scope">
                  <span>{{ scope.row.homeOfProvince }} {{ scope.row.homeOfCity }} {{ scope.row.homeOfArea }} {{ scope.row.homeOfCounty }} {{ scope.row.homeOfDetail }}</span>
                </template>
              </el-table-column>
              <el-table-column width="140" label="是否与户籍地址一致" align="center" prop="isTheSameAsHousehold" :show-overflow-tooltip="true" >
                <template slot-scope="scope">
                  <span>{{ scope.row.isTheSameAsHousehold === 'true' ? '是' : '否'  }}</span>
                </template>
              </el-table-column>
              <el-table-column width="140" label="家庭住址邮政编码" align="center" prop="homeOfPostcode" :show-overflow-tooltip="true" />
              <el-table-column width="120" label="家庭电话" align="center" prop="homeOfPhone" :show-overflow-tooltip="true" />
            </el-table-column>
            <el-table-column label="就学信息" align="center" :show-overflow-tooltip="true" >
              <el-table-column label="高校名称" align="center" prop="collegeOfName" :show-overflow-tooltip="true" />
              <el-table-column width="160" label="院系名称" align="center" prop="collegeOfInstitute" :show-overflow-tooltip="true" />
              <el-table-column label="专业名称" align="center" prop="collegeOfMajor" :show-overflow-tooltip="true" />
              <el-table-column label="入学时间" align="center" prop="collegeWithStart" width="180">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.collegeWithStart) }}</span>
                </template>
              </el-table-column>
              <el-table-column label="学历" align="center" prop="collegeOfEducation" :show-overflow-tooltip="true" />
              <el-table-column width="120" label="学生学号" align="center" prop="studentNo" :show-overflow-tooltip="true" />
              <el-table-column label="专业类别" align="center" prop="typeOfMajor" :show-overflow-tooltip="true" />
              <el-table-column label="学制" align="center" prop="collegeOfEducationalSystem" :show-overflow-tooltip="true" />
              <el-table-column label="毕业时间" align="center" prop="collegeWithEnd" width="120" />
            </el-table-column>
            <el-table-column width="120" label="资格审查确认" align="center" :show-overflow-tooltip="true" >
              <el-table-column width="120" label="资格审查单位" align="center" prop="countyUnit" :show-overflow-tooltip="true" />
              <el-table-column width="120" label="是否盖章" align="center" prop="isSeal" :show-overflow-tooltip="true" />
              <el-table-column width="120" label="盖章日期" align="center" prop="sealDate" :show-overflow-tooltip="true" />
              <el-table-column width="120" label="学生签字日期" align="center" prop="signatureDate"/>
            </el-table-column>
            <el-table-column label="就业信息" align="center" :show-overflow-tooltip="true" >
              <el-table-column label="工作单位" align="center" prop="workFeature" :show-overflow-tooltip="true" />
              <el-table-column label="单位性质" align="center" prop="phoneOfUnit" :show-overflow-tooltip="true" />
              <el-table-column label="单位电话" align="center" prop="postCodeOfUnit" :show-overflow-tooltip="true" />
              <el-table-column width="140" label="单位邮政编码" align="center" prop="addressOfUnit" :show-overflow-tooltip="true" />
              <el-table-column label="单位地址" align="center" prop="addressOfUnit" :show-overflow-tooltip="true" />
            </el-table-column>
            <el-table-column label="变更" align="center" prop="changeForReason" :show-overflow-tooltip="true" />
          </el-table>

        </el-card>
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名称" prop="nickName">
              {{form.userName}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户昵称" prop="nickName">
              {{form.nickName}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phonenumber">
              {{form.phonenumber}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              {{form.email}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item v-if="form.userId == undefined" label="用户名称" prop="userName">
              {{form.userName}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.userId == undefined" label="用户密码" prop="password">
              {{form.password}}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { getUser, delUser, addUser, updateUser, exportUser, resetUserPwd, importTemplate } from "@/api/system/user";
  import { getToken } from "@/utils/auth";
  import { treeselect } from "@/api/system/dept";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import { selectUserListForStudent } from '../../../api/system/user'
  import { listPayOrderData } from '../../../api/alipay/repay'
  import { getCredits, listData } from '../../../api/county/credit'

  export default {
    name: "User",
    components: { Treeselect },
    data() {
      return {
        creditList: [],
        payOrderList: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        total2: 0,
        total3: 0,
        // 用户表格数据
        userList: null,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 角色选项
        roleOptions: [],
        // 表单参数
        form: {},
        // 用户导入参数
        upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/system/user/importData"
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          phonenumber: undefined,
        },
        // 表单校验
        rules: {},
      };
    },
    watch: {
      // 根据名称筛选部门树
      deptName(val) {
        this.$refs.tree.filter(val);
      }
    },
    created() {
      this.getList();
      this.getTreeselect();
    },
    methods: {
      rowClick(data) {
        console.log(data)
        getCredits(data.userName).then(response => {
          this.creditList = response.data
          console.log('通过用户名查询')
          console.log(response)
          this.total3 = response.data.length
          this.loading = false;
        });
        this.payOrderList = data.payOrderList
      },
/*      getPayOrderList() {
        listPayOrderData(this.queryParams).then(response => {
          this.payOrderList = response.rows
          console.log(response)
          this.total2 = response.total
          this.loading = false
        });
      },*/
      /** 查询用户列表 */
      getList() {
        this.loading = true
        //listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        selectUserListForStudent().then(response => {
          this.userList = response.userListForStudent;
          console.log(response)
          this.total = response.userListForStudent.length;
          this.loading = false;
        })
      },
      /** 查询部门下拉树结构 */
      getTreeselect() {
        treeselect().then(response => {
          this.deptOptions = response.data;
        });
      },
      // 筛选节点
      filterNode(value, data) {
        if (!value) return true;
        return data.label.indexOf(value) !== -1;
      },
      // 节点单击事件
      handleNodeClick(data) {
        this.queryParams.deptId = data.id;
        this.getList();
      },
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          userId: undefined,
          deptId: undefined,
          userName: undefined,
          nickName: undefined,
          password: undefined,
          phonenumber: undefined,
          email: undefined,
          sex: undefined,
          status: "0",
          remark: undefined,
          postIds: [],
          roleIds: []
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.userId);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.getTreeselect();
        getUser().then(response => {
          this.open = true;
          this.title = "添加用户";
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.getTreeselect();
        const userId = row.userId || this.ids;
        getUser(userId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改用户";
          this.form.password = "";
        });
      },
      /** 重置密码按钮操作 */
      handleResetPwd(row) {
        this.$prompt('请输入"' + row.userName + '"的新密码', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消"
        }).then(({ value }) => {
          resetUserPwd(row.userId, value).then(response => {
            if (response.code === 200) {
              this.msgSuccess("修改成功，新密码是：" + value);
            }
          });
        }).catch(() => {});
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.userId != undefined) {
              updateUser(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addUser(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const userIds = row.userId || this.ids;
        this.$confirm('是否确认删除用户编号为"' + userIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delUser(userIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有用户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportUser(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      },
      /** 导入按钮操作 */
      handleImport() {
        this.upload.title = "用户导入";
        this.upload.open = true;
      },
      /** 下载模板操作 */
      importTemplate() {
        importTemplate().then(response => {
          this.download(response.msg);
        });
      },
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true;
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        this.upload.open = false;
        this.upload.isUploading = false;
        this.$refs.upload.clearFiles();
        this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
        this.getList();
      },
      // 提交上传文件
      submitFileForm() {
        this.$refs.upload.submit();
      }
    }
  };
</script>
